<?php

if (!isset($_SESSION)) {
    session_start();
}

require('../../Smarty/libs/Smarty.class.php');

$smarty = new Smarty;

$smarty->setTemplateDir('./smarty/templates/');
$smarty->setCompileDir('./smarty/templates_c/');
$smarty->setConfigDir('./smarty/configs/');
$smarty->setCacheDir('./smarty/cache/');

if (empty($_SESSION['logged_bool'])) {
    $_SESSION['logged_bool'] = false;
}
$status = $_SESSION['logged_bool'];

/*
 * If user is not logged in the following code sends him to the login page
 * Together with all the variables in the _POST superglobal 
 */
if ($_SESSION['logged_bool'] == false) {
    /* Copy _POST contents to _SESSION */
    $_SESSION['login_guard']['postcontents'] = $_POST;
    $_SESSION['currentPage'] = $_POST['currentPage'];
    header('Location: login.php');
    exit();
}

if (isset($_POST)) {
    if (isset($_POST["step"])) {
        $stepOneCompleted = (boolean) $_POST["step"];
    } else {
        /*
    * Copy session contents to the _POST superglobal IF the currentPage
    * Session variable is set
    */
        if ((!empty($_SESSION['currentPage'])) && ($_SESSION['currentPage'] == 'bookingPage.php')) {
            $_POST = $_SESSION['login_guard']['postcontents'];
        }

        if ((!empty($_SESSION['book_guard']['postcontents']['currentPage'])) && ($_SESSION['book_guard']['postcontents']['currentPage'] == 'bookingPage.php')) {
            $_POST = $_SESSION['book_guard']['postcontents'];
            unset($_SESSION['book_guard']);
        }
        $stepOneCompleted = false;
    }
}




$contactinfo = array();
$roomprices = array();

if (isset($_SESSION["roomsNumber"])) {
    $guestsCount = (int) $_SESSION["roomsNumber"];
} else {
    $guestsCount = 0;
}
/*
$guestsinfo = array();
for ($i = 1; $i <= $guestsCount; $i++) {
    $guestsinfo[$i]["firstname"] = isset($_POST["guest_firstName$i"]) ? $_POST["guest_firstName$i"] : "";
    $guestsinfo[$i]["lastname"] = isset($_POST["guest_lastName$i"]) ? $_POST["guest_lastName$i"] : "";
    $guestsinfo[$i]["smokingPreference"] = isset($_SESSION["room$i" . "_smokingPreference"]) ? $_SESSION["room$i" . "_smokingPreference"] : "";
    $guestsinfo[$i]["adults"] = isset($_SESSION["room$i" . "_adults"]) ? $_SESSION["room$i" . "_adults"] : "";
    $guestsinfo[$i]["children"] = isset($_SESSION["room$i" . "_children"]) ? $_SESSION["room$i" . "_children"] : "";
}*/

if ($stepOneCompleted) { // true
    //$hotelinfo = $_SESSION["selectedRoom"];
    for ($i = 1; $i <= $guestsCount; $i++) {
        $hotelinfo["guests"][$i]["firstname"] = isset($_POST["guest_firstName$i"]) ? $_POST["guest_firstName$i"] : "";
        $hotelinfo["guests"][$i]["lastname"] = isset($_POST["guest_lastName$i"]) ? $_POST["guest_lastName$i"] : "";
        $hotelinfo["guests"][$i]["smokingPreference"] = isset($_SESSION["room$i" . "_smokingPreference"]) ? $_SESSION["room$i" . "_smokingPreference"] : "";
    }

    $email = isset($_POST["contactDetails_email"]) ? $_POST["contactDetails_email"] : "";
    $confirmEmail = isset($_POST["contactDetails_confirmEmail"]) ? $_POST["contactDetails_confirmEmail"] : "";
    $phone = isset($_POST["contactDetails_phoneNumber"]) ? $_POST["contactDetails_phoneNumber"] : "";

    $_SESSION["contactDetail"]["rooms"] = $hotelinfo["guests"];
    $_SESSION["contactDetail"]["email"] = $email;
    $_SESSION["contactDetail"]["confirmEmail"] = $confirmEmail;
    $_SESSION["contactDetail"]["phone"] = $phone;
    $_SESSION["contactDetail"]["smokingPreference"] = "";
    $_SESSION["contactDetail"]["special"] = "";
    //$hotelinfo = $_SESSION["selectedRoom"];

    // connect to the databse to receive countries
    mysql_connect("localhost:3306", "root", "expedia") or die("Unable to connect to MySQL " . mysql_error());
    @mysql_select_db("eandb") or die("Unable to select database " . mysql_error());
    
    //$qstring = "SELECT c.CountryName, c.CountryCode FROM countrylist AS c;";
    $qstring ="SELECT c.CountryName, c.CountryCode FROM countrylist as c ORDER BY c.CountryName ASC;";
    $result = mysql_query($qstring) or die(mysql_error()); //query the database for entries containing the term
    $countries = array();
    $index=0;
    /* Check to see if the destination exists */
    while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
        $countries[$index++] =$row;
    }
    
    // states
    // US = 201
    // CA = 31
    $qstring ="SELECT s.name as StateName, s.abbreviation as StateCode, s.country_id as CountryId FROM states as s;";
    $result = mysql_query($qstring) or die(mysql_error()); //query the database for entries containing the term
    $states = array();
    $index=0;
    /* Check to see if the destination exists */
    while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
        $states[$index++] = $row;
    }
    

    $contactinfo = $_SESSION["contactDetail"];
    $smarty->assign("contactinfo", $contactinfo);
    $smarty->assign("states", $states);
    $smarty->assign("countries", $countries);
    $smarty->assign("cancellationPolicy", "");
    $smarty->assign("book_file", "booking_step_two.tpl");
} else {    // false
    /*
     * Getting post contents 
     */
    $hotelThumbnail = isset($_POST["hotelImage"]) ? $_POST["hotelImage"] : $_SESSION["selectedRoom"]["thumbnailUrl"];
    $hotelName = isset($_POST["hotelName"]) ? $_POST["hotelName"] : $_SESSION["selectedRoom"]["hotelName"];
    $checkin = isset($_POST["checkin"]) ? $_POST["checkin"] : $_SESSION["selectedRoom"]["checkin"];
    $checkout = isset($_POST["checkout"]) ? $_POST["checkout"] : $_SESSION["selectedRoom"]["checkout"];
    $roomDescription = isset($_POST["roomDescription"]) ? $_POST["roomDescription"] : $_SESSION["selectedRoom"]["roomTypeString"];
    $smokingPreferences = isset($_POST["smokingPreferences"]) ? $_POST["smokingPreferences"] : $_SESSION["selectedRoom"]["smokingPreferences"];
    $total = isset($_POST["total"]) ? $_POST["total"] : $_SESSION["selectedRoom"]["total"];
    $surchargeTotal = isset($_POST["surchargeTotal"]) ? (float) $_POST["surchargeTotal"] : $_SESSION["selectedRoom"]["surchargeTotal"];
    $currencyCode = isset($_POST["currencyCode"]) ? $_POST["currencyCode"] : $_SESSION["selectedRoom"]["currencyCode"];
    $rateCode = isset($_POST["roomRateCode"]) ? $_POST["roomRateCode"] : $_SESSION["selectedRoom"]["rateCode"];
    $roomTypeCode = isset($_POST["roomCode"]) ? $_POST["roomCode"] : $_SESSION["selectedRoom"]["roomTypeCode"];
    if($roomTypeCode == ""){
        $roomTypeCode = isset($_POST["roomTypeCode"]) ? $_POST["roomTypeCode"] : $_SESSION["selectedRoom"]["roomTypeCode"];
    }
    $minGuestAge = isset($_POST["minGuestAge"]) ? (int) $_POST["minGuestAge"] : $_SESSION["selectedRoom"]["minGuestAge"];
    $currentAllotment = isset($_POST["currentAllotment"]) ? (int) $_POST["currentAllotment"] : $_SESSION["selectedRoom"]["currentAllotment"];
    $nonRefundable = isset($_POST["nonRefundable"]) ? (boolean) $_POST["nonRefundable"] : $_SESSION["selectedRoom"]["nonRefundable"];
    $rateKey = isset($_POST["roomRateKey"]) ? $_POST["roomRateKey"] : $_SESSION["selectedRoom"]["rateKey"];
    $averageRate = isset($_POST["averageRate"]) ? $_POST["averageRate"] : $_SESSION["selectedRoom"]["averageRate"];
    $nightlyRateTotal = isset($_POST["nightlyRateTotal"]) ? $_POST["nightlyRateTotal"] : $_SESSION["selectedRoom"]["nightlyRatesTotal"];
    $hotelId = isset($_POST["hotelId"]) ? $_POST["hotelId"] : $_SESSION["selectedRoom"]["hotelId"];
    $supplierType = isset($_POST["supplierType"]) ? $_POST["supplierType"] : $_SESSION["selectedRoom"]["supplierType"];
    $cancellationPolicy = isset($_POST["cancellationPolicy"]) ? $_POST["cancellationPolicy"] : $_SESSION["selectedRoom"]["cancellationPolicy"];
    //
    $rooms = isset($_SESSION["rooms"]) ? $_SESSION["rooms"] : array(1 => array("adults" => 0, "children" => 0, "ages_array" => array(), "ages_string" => ""));
    foreach ($rooms as $key => $value) {
        $rooms[$key]["firstname"] = "";
        $rooms[$key]["lastname"] = "";
        $rooms[$key]["smokingPreference"] = "";
    }
    $roomCount = count($rooms);

    /* PUTTING POST IN SESSION */
    $_SESSION["selectedRoom"]["checkin"] = $checkin;
    $_SESSION["selectedRoom"]["checkout"] = $checkout;
    $_SESSION["selectedRoom"]["rateCode"] = $rateCode;
    $_SESSION["selectedRoom"]["roomTypeCode"] = $roomTypeCode;
    //$_SESSION["selectedRoom"]["roomTypeDescription"]    = $roomDescription;
    $_SESSION["selectedRoom"]["roomTypeString"] = $roomDescription;
    $_SESSION["selectedRoom"]["minGuestAge"] = $minGuestAge;
    $_SESSION["selectedRoom"]["nonRefundable"] = $nonRefundable;
    $_SESSION["selectedRoom"]["currentAllotment"] = $currentAllotment;
    $_SESSION["selectedRoom"]["rateKey"] = $rateKey;
    $_SESSION["selectedRoom"]["averageRate"] = $averageRate;
    $_SESSION["selectedRoom"]["total"] = $total;
    $_SESSION["selectedRoom"]["currencyCode"] = $currencyCode;
    $_SESSION["selectedRoom"]["nightlyRatesTotal"] = $nightlyRateTotal;
    $_SESSION["selectedRoom"]["smokingPreferences"] = $smokingPreferences;
    $_SESSION["selectedRoom"]["surchargeTotal"] = $surchargeTotal;
    $_SESSION["selectedRoom"]["hotelId"] = $hotelId;
    //$_SESSION["selectedRoom"]["checkInInstructions"]    =
    $_SESSION["selectedRoom"]["thumbnailUrl"] = $hotelThumbnail;
    $_SESSION["selectedRoom"]["hotelName"] = $hotelName;
    $_SESSION["selectedRoom"]["supplierType"] = $supplierType;
    $_SESSION["selectedRoom"]["cancellationPolicy"] = $cancellationPolicy;
    $_SESSION["selectedRoom"]["roomcount"] = $roomCount;

    unset($_SESSION["contactDetail"]);

    $_SESSION["contactDetail"]["rooms"] = isset($_SESSION["contactDetail"]["rooms"]) ? $_SESSION["contactDetail"]["rooms"] : $rooms;
    $_SESSION["contactDetail"]["email"] = isset($_SESSION["contactDetail"]["email"]) ? $_SESSION["contactDetail"]["email"] : "";
    $_SESSION["contactDetail"]["confirmEmail"] = isset($_SESSION["contactDetail"]["confirmEmail"]) ? $_SESSION["contactDetail"]["confirmEmail"] : "";
    $_SESSION["contactDetail"]["phone"] = isset($_SESSION["contactDetail"]["confirmEmail"]) ? $_SESSION["contactDetail"]["confirmEmail"] : "";
    $_SESSION["contactDetail"]["smokingPreference"] = "";
    $_SESSION["contactDetail"]["special"] = "";

    $bookData = $_SESSION["contactDetail"];
    //$hotelinfo = $_SESSION["selectedRoom"];
    $smarty->assign("book", $bookData);
    $smarty->assign("book_file", "booking_step_one.tpl");
}
$hotelinfo = $_SESSION["selectedRoom"];
$smarty->assign("hotelinfo", $hotelinfo);
$smarty->assign("logged", $status);
$smarty->assign("stepOneCompleted",$stepOneCompleted);
$smarty->display("preHeader.tpl");
echo "<body>";
$smarty->display("header.tpl");
$smarty->display("booking_page.tpl");
echo "</body>";
?>